﻿/*
Commerce by Onex – eCommerce software platform (www.ecommerce.byonex.com)
Copyright (C) 2008-2010 by Onex Group

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
 */
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Resources;
using System.Xml;
using System.Text;
using ShopModel;
using ShopBLL;
using System.IO;
using System.Web.Configuration;

public partial class cms_Scheme : System.Web.UI.Page
{
    #region CONSTANTS

    private const string LANGUAGE_ID = "EditLanguageID";
    private const string DEFAULT_LANGUAGE_CULTURE = "DefaultLanguageCulture";
    private const string EDIT_LANGUAGE_CULTURE = "EditLanguageCulture";
    private const string EDITED_PARTNER = "EditedPartner";
    private const string ACTIVE_TAB = "ActiveTabIndex";

    const string ShopContentShopPrefix = "ShopContentShop";

    const string ShopContentAboutUrlPrefix = "ShopContentAboutUrl";
    const string ShopContentAboutPrefix = "ShopContentAbout";

    const string ShopContentContactUrlPrefix = "ShopContentContactUrl";
    const string ShopContentContactPrefix = "ShopContentContact";

    const string ShopContentDeliveryUrlPrefix = "ShopContentDeliveryUrl";
    const string ShopContentDeliveryPrefix = "ShopContentDelivery";

    const string ShopContentShipmentUrlPrefix = "ShopContentShipmentUrl";
    const string ShopContentShipmentPrefix = "ShopContentShipment";

    const string ShopContentInstalmentsUrlPrefix = "ShopContentInstalmentsUrl";
    const string ShopContentInstalmentsPrefix = "ShopContentInstalments";

    const string ShopContentRegulationUrlPrefix = "ShopContentRegulationUrl";
    const string ShopContentRegulationPrefix = "ShopContentRegulation";

    const string ShopContentOfferUrlPrefix = "ShopContentOfferUrl";
    const string ShopContentOfferPrefix = "ShopContentOffer";

    const string NotificationOrderBodyPrefix = "NotificationOrderBody";
    const string NotificationOrderHeaderPrefix = "NotificationOrderHeader";

    const string NotificationSendBodyPrefix = "NotificationSendBody";
    const string NotificationSendHeaderPrefix = "NotificationSendHeader";

    const string NotificationPaymentBodyPrefix = "NotificationPaymentBody";
    const string NotificationPaymentHeaderPrefix = "NotificationPaymentHeader";

    const string NotificationOnlineBodyPrefix = "NotificationOnlineBody";
    const string NotificationOnlineHeaderPrefix = "NotificationOnlineHeader";

    const string NotificationDelayedBodyPrefix = "NotificationDelayedBody";
    const string NotificationDelayedHeaderPrefix = "NotificationDelayedHeader";

    const string NotificationActivateBodyPrefix = "NotificationActivateBody";
    const string NotificationActivateHeaderPrefix = "NotificationActivateHeader";

    const string NotificationConfirmBodyPrefix = "NotificationConfirmBody";
    const string NotificationConfirmHeaderPrefix = "NotificationConfirmHeader";

    const string NotificationVoucherBodyPrefix = "NotificationVoucherBody";
    const string NotificationVoucherHeaderPrefix = "NotificationVoucherHeader";

    const string NotificationResetBodyPrefix = "NotificationResetBody";
    const string NotificationResetHeaderPrefix = "NotificationResetHeader";

    const string TitleTagPrefix = "TitleTagPrefix";
    const string MetaTagDescriptionPrefix = "MetaTagDescriptionPrefix";
    const string MetaTagKeywordsPrefix = "MetaTagKeywordsPrefix";
    const string PageFooterPrefix = "PageFooterPrefix";

    #endregion CONSTANTS

    #region VARIABLES

    protected List<ReportAdressee> ReportMailingList = null;

    #endregion VARIABLES

    #region METHODS

    protected void _DataBind()
    {
        tbCompanyName.Text = ShopModel.Settings.CompanyName;
        tbCompanyOwner.Text = ShopModel.Settings.CompanyOwner;
        tbCompanyAccount.Text = ShopModel.Settings.CompanyAccount;
        tbCompanyNIP.Text = ShopModel.Settings.CompanyNIP;
        tbCompanyStreet.Text = ShopModel.Settings.CompanyStreet;
        tbCompanyTown.Text = ShopModel.Settings.CompanyTown;
        tbCompanyZipCode.Text = ShopModel.Settings.CompanyZipCode;
        tbCompanyPhoneNumber.Text = ShopModel.Settings.CompanyPhoneNumber;
        tbCompanyEmail.Text = ShopModel.Settings.CompanyEmail;
        tbCompanyOnlineContact.Text = ShopModel.Settings.CompanyOnlineContact;

        tbCompanyMailHost.Text = ShopModel.Settings.CompanyMailHost;
        tbCompanyMailLogin.Text = ShopModel.Settings.CompanyMailLogin;
        tbCompanyMailPassword.Attributes["value"] = ShopModel.Settings.CompanyMailPassword;
        tbCompanyMailDisplayedName.Text = ShopModel.Settings.CompanyEmailDisplayName;

        tbShipmentAgreement.Text = ShopModel.Settings.ShipmentAgreement;

        tbInvoiceFooter.Text = ShopModel.Settings.InvoiceFooter;
        tbInvoiceNumberMask.Text = ShopModel.Settings.InvoiceNumberMask;

        cbMailConfirmation.Checked = ShopModel.Settings.RegistrationConfirmationMail;

        tbAvailableCountRange.Text = ShopModel.Settings.AvailableCountRange.ToString();
        tbInvoiceLastNumber.Text = OrderManager.Instance.GetInvoiceNumber().ToString();
        tbVatAmount.Text = (Settings.ShipmentVatRateValue * (decimal)100).ToString();

        cbDisplayingProductscomparison.Checked = Settings.ShowProductsToCompare;
        cbDisplayingBestsellers.Checked = Settings.ShowBestsellers;
        cbDisplayingNews.Checked = Settings.ShowNews;
        cbDisplayingPartners.Checked = Settings.ShowPartners;
        cbShowAvailableCount.Checked = Settings.ShowAvailableCount;
        cbShowCrossSell.Checked = Settings.ShowCrossSell;

        XmlDocument xmlDocument;

        string XmlDirectory = Settings.TemplatesDirectory + "/";

        try
        {

            foreach (ListItem li in ddlShopContentLanguage.Items)
            {
                xmlDocument = new XmlDocument();
                if (li.Value == (string)Session[DEFAULT_LANGUAGE_CULTURE])
                    xmlDocument.Load(Server.MapPath(XmlDirectory + "ContentTemplates.xml"));
                else
                    xmlDocument.Load(Server.MapPath(XmlDirectory + "ContentTemplates." + li.Value + ".xml"));

                foreach (XmlNode _XmlNode in xmlDocument["ContentTemplates"])
                {
                    if (_XmlNode.Name == "Shop")
                    {
                        foreach (XmlNode _XmlChildNode in _XmlNode.ChildNodes)
                        {
                            if (_XmlChildNode.Name == "Content")
                            {
                                Session[ShopContentShopPrefix + li.Value] = _XmlChildNode.ChildNodes.Count > 0 ? _XmlChildNode.ChildNodes[0].Value : string.Empty;
                            }
                        }
                    }

                    if (_XmlNode.Name == "About")
                    {
                        foreach (XmlNode _XmlChildNode in _XmlNode.ChildNodes)
                        {
                            if (_XmlChildNode.Name == "Content")
                            {
                                Session[ShopContentAboutPrefix + li.Value] = _XmlChildNode.ChildNodes.Count > 0 ? _XmlChildNode.ChildNodes[0].Value : string.Empty;
                            }

                            if (_XmlChildNode.Name == "Url")
                            {
                                Session[ShopContentAboutUrlPrefix + li.Value] = _XmlChildNode.ChildNodes.Count > 0 ? _XmlChildNode.ChildNodes[0].Value : string.Empty;
                            }
                        }
                    }


                    if (_XmlNode.Name == "Contact")
                    {
                        foreach (XmlNode _XmlChildNode in _XmlNode.ChildNodes)
                        {
                            if (_XmlChildNode.Name == "Content")
                            {
                                Session[ShopContentContactPrefix + li.Value] = _XmlChildNode.ChildNodes.Count > 0 ? _XmlChildNode.ChildNodes[0].Value : string.Empty;
                            }

                            if (_XmlChildNode.Name == "Url")
                            {
                                Session[ShopContentContactUrlPrefix + li.Value] = _XmlChildNode.ChildNodes.Count > 0 ? _XmlChildNode.ChildNodes[0].Value : string.Empty;
                            }
                        }
                    }

                    if (_XmlNode.Name == "Delivery")
                    {
                        foreach (XmlNode _XmlChildNode in _XmlNode.ChildNodes)
                        {
                            if (_XmlChildNode.Name == "Content")
                            {
                                Session[ShopContentDeliveryPrefix + li.Value] = _XmlChildNode.ChildNodes.Count > 0 ? _XmlChildNode.ChildNodes[0].Value : string.Empty;
                            }

                            if (_XmlChildNode.Name == "Url")
                            {
                                Session[ShopContentDeliveryUrlPrefix + li.Value] = _XmlChildNode.ChildNodes.Count > 0 ? _XmlChildNode.ChildNodes[0].Value : string.Empty;
                            }
                        }
                    }

                    if (_XmlNode.Name == "Shipment")
                    {
                        foreach (XmlNode _XmlChildNode in _XmlNode.ChildNodes)
                        {
                            if (_XmlChildNode.Name == "Content")
                            {
                                Session[ShopContentShipmentPrefix + li.Value] = _XmlChildNode.ChildNodes.Count > 0 ? _XmlChildNode.ChildNodes[0].Value : string.Empty;
                            }

                            if (_XmlChildNode.Name == "Url")
                            {
                                Session[ShopContentShipmentUrlPrefix + li.Value] = _XmlChildNode.ChildNodes.Count > 0 ? _XmlChildNode.ChildNodes[0].Value : string.Empty;
                            }
                        }
                    }

                    if (_XmlNode.Name == "Instalments")
                    {
                        foreach (XmlNode _XmlChildNode in _XmlNode.ChildNodes)
                        {
                            if (_XmlChildNode.Name == "Content")
                            {
                                Session[ShopContentInstalmentsPrefix + li.Value] = _XmlChildNode.ChildNodes.Count > 0 ? _XmlChildNode.ChildNodes[0].Value : string.Empty;
                            }

                            if (_XmlChildNode.Name == "Url")
                            {
                                Session[ShopContentInstalmentsUrlPrefix + li.Value] = _XmlChildNode.ChildNodes.Count > 0 ? _XmlChildNode.ChildNodes[0].Value : string.Empty;
                            }
                        }
                    }

                    if (_XmlNode.Name == "Regulation")
                    {
                        foreach (XmlNode _XmlChildNode in _XmlNode.ChildNodes)
                        {
                            if (_XmlChildNode.Name == "Content")
                            {
                                Session[ShopContentRegulationPrefix + li.Value] = _XmlChildNode.ChildNodes.Count > 0 ? _XmlChildNode.ChildNodes[0].Value : string.Empty;
                            }

                            if (_XmlChildNode.Name == "Url")
                            {
                                Session[ShopContentRegulationUrlPrefix + li.Value] = _XmlChildNode.ChildNodes.Count > 0 ? _XmlChildNode.ChildNodes[0].Value : string.Empty;
                            }
                        }
                    }

                    if (_XmlNode.Name == "Offer")
                    {
                        foreach (XmlNode _XmlChildNode in _XmlNode.ChildNodes)
                        {
                            if (_XmlChildNode.Name == "Content")
                            {
                                Session[ShopContentOfferPrefix + li.Value] = _XmlChildNode.ChildNodes.Count > 0 ? _XmlChildNode.ChildNodes[0].Value : string.Empty;
                            }

                            if (_XmlChildNode.Name == "Url")
                            {
                                Session[ShopContentOfferUrlPrefix + li.Value] = _XmlChildNode.ChildNodes.Count > 0 ? _XmlChildNode.ChildNodes[0].Value : string.Empty;
                            }
                        }
                    }

                    if (_XmlNode.Name == "TitleTag")
                    {
                        foreach (XmlNode _XmlChildNode in _XmlNode.ChildNodes)
                        {
                            if (_XmlChildNode.Name == "Content")
                            {
                                Session[TitleTagPrefix + li.Value] = _XmlChildNode.ChildNodes.Count > 0 ? _XmlChildNode.ChildNodes[0].Value : string.Empty;
                            }
                        }
                    }

                    if (_XmlNode.Name == "MetaTagDescription")
                    {
                        foreach (XmlNode _XmlChildNode in _XmlNode.ChildNodes)
                        {
                            if (_XmlChildNode.Name == "Content")
                            {
                                Session[MetaTagDescriptionPrefix + li.Value] = _XmlChildNode.ChildNodes.Count > 0 ? _XmlChildNode.ChildNodes[0].Value : string.Empty;
                            }
                        }
                    }

                    if (_XmlNode.Name == "MetaTagKeywords")
                    {
                        foreach (XmlNode _XmlChildNode in _XmlNode.ChildNodes)
                        {
                            if (_XmlChildNode.Name == "Content")
                            {
                                Session[MetaTagKeywordsPrefix + li.Value] = _XmlChildNode.ChildNodes.Count > 0 ? _XmlChildNode.ChildNodes[0].Value : string.Empty;
                            }
                        }
                    }

                    if (_XmlNode.Name == "PageFooter")
                    {
                        foreach (XmlNode _XmlChildNode in _XmlNode.ChildNodes)
                        {
                            if (_XmlChildNode.Name == "Content")
                            {
                                Session[PageFooterPrefix + li.Value] = _XmlChildNode.ChildNodes.Count > 0 ? _XmlChildNode.ChildNodes[0].Value : string.Empty;
                            }
                        }
                    }
                }
            }
        }
        catch
        { }
        LoadNotificationTemplates();
        gvAdressesListDataBind();
        PartnersDataBind();
    }

    protected void LoadNotificationTemplates()
    {
        XmlDocument xmlDocument;

        string XmlDirectory = Settings.TemplatesDirectory + "/";

        try
        {

            foreach (ListItem li in ddlNotifLanguage.Items)
            {
                xmlDocument = new XmlDocument();
                if (li.Value == (string)Session[DEFAULT_LANGUAGE_CULTURE])
                    xmlDocument.Load(Server.MapPath(XmlDirectory + "NotificationTemplates.xml"));
                else
                    xmlDocument.Load(Server.MapPath(XmlDirectory + "NotificationTemplates." + li.Value + ".xml"));

                foreach (XmlNode _XmlNode in xmlDocument["NotificationTemplates"])
                {
                    if (_XmlNode.Name == "OrderConfirmation")
                    {
                        foreach (XmlNode _XmlChildNode in _XmlNode.ChildNodes)
                        {
                            if (_XmlChildNode.Name == "Header")
                            {
                                Session[NotificationOrderHeaderPrefix + li.Value] = _XmlChildNode.ChildNodes.Count > 0 ? _XmlChildNode.ChildNodes[0].Value : string.Empty;
                            }

                            if (_XmlChildNode.Name == "Body")
                            {
                                Session[NotificationOrderBodyPrefix + li.Value] = _XmlChildNode.ChildNodes.Count > 0 ? _XmlChildNode.ChildNodes[0].Value : string.Empty;
                            }
                        }
                    }

                    if (_XmlNode.Name == "SendConfirmation")
                    {
                        foreach (XmlNode _XmlChildNode in _XmlNode.ChildNodes)
                        {
                            if (_XmlChildNode.Name == "Header")
                            {
                                Session[NotificationSendHeaderPrefix + li.Value] = _XmlChildNode.ChildNodes.Count > 0 ? _XmlChildNode.ChildNodes[0].Value : string.Empty;
                            }

                            if (_XmlChildNode.Name == "Body")
                            {
                                Session[NotificationSendBodyPrefix + li.Value] = _XmlChildNode.ChildNodes.Count > 0 ? _XmlChildNode.ChildNodes[0].Value : string.Empty;
                            }
                        }
                    }


                    if (_XmlNode.Name == "PaymentNotification")
                    {
                        foreach (XmlNode _XmlChildNode in _XmlNode.ChildNodes)
                        {
                            if (_XmlChildNode.Name == "Header")
                            {
                                Session[NotificationPaymentHeaderPrefix + li.Value] = _XmlChildNode.ChildNodes.Count > 0 ? _XmlChildNode.ChildNodes[0].Value : string.Empty;
                            }

                            if (_XmlChildNode.Name == "Body")
                            {
                                Session[NotificationPaymentBodyPrefix + li.Value] = _XmlChildNode.ChildNodes.Count > 0 ? _XmlChildNode.ChildNodes[0].Value : string.Empty;
                            }
                        }
                    }

                    if (_XmlNode.Name == "OnlinePaymentNotification")
                    {
                        foreach (XmlNode _XmlChildNode in _XmlNode.ChildNodes)
                        {
                            if (_XmlChildNode.Name == "Header")
                            {
                                Session[NotificationOnlineHeaderPrefix + li.Value] = _XmlChildNode.ChildNodes.Count > 0 ? _XmlChildNode.ChildNodes[0].Value : string.Empty;
                            }

                            if (_XmlChildNode.Name == "Body")
                            {
                                Session[NotificationOnlineBodyPrefix + li.Value] = _XmlChildNode.ChildNodes.Count > 0 ? _XmlChildNode.ChildNodes[0].Value : string.Empty;
                            }
                        }
                    }
                    //----------------------------
                    if (_XmlNode.Name == "DelayedPaymentNotification")
                    {
                        foreach (XmlNode _XmlChildNode in _XmlNode.ChildNodes)
                        {
                            if (_XmlChildNode.Name == "Header")
                            {
                                Session[NotificationDelayedHeaderPrefix + li.Value] = _XmlChildNode.ChildNodes.Count > 0 ? _XmlChildNode.ChildNodes[0].Value : string.Empty;
                            }

                            if (_XmlChildNode.Name == "Body")
                            {
                                Session[NotificationDelayedBodyPrefix + li.Value] = _XmlChildNode.ChildNodes.Count > 0 ? _XmlChildNode.ChildNodes[0].Value : string.Empty;
                            }
                        }
                    }

                    //powiadomienie o rejestracji
                    if (_XmlNode.Name == "AccountCreatedNotification")
                    {
                        foreach (XmlNode _XmlChildNode in _XmlNode.ChildNodes)
                        {
                            if (_XmlChildNode.Name == "Header")
                            {
                                Session[NotificationConfirmHeaderPrefix + li.Value] = _XmlChildNode.ChildNodes.Count > 0 ? _XmlChildNode.ChildNodes[0].Value : string.Empty;
                            }

                            if (_XmlChildNode.Name == "Body")
                            {
                                Session[NotificationConfirmBodyPrefix + li.Value] = _XmlChildNode.ChildNodes.Count > 0 ? _XmlChildNode.ChildNodes[0].Value : string.Empty;
                            }
                        }
                    }

                    //powiadomienie o aktywacji konta
                    if (_XmlNode.Name == "AccountActivatedNotification")
                    {
                        foreach (XmlNode _XmlChildNode in _XmlNode.ChildNodes)
                        {
                            if (_XmlChildNode.Name == "Header")
                            {
                                Session[NotificationActivateHeaderPrefix + li.Value] = _XmlChildNode.ChildNodes.Count > 0 ? _XmlChildNode.ChildNodes[0].Value : string.Empty;
                            }

                            if (_XmlChildNode.Name == "Body")
                            {
                                Session[NotificationActivateBodyPrefix + li.Value] = _XmlChildNode.ChildNodes.Count > 0 ? _XmlChildNode.ChildNodes[0].Value : string.Empty;
                            }
                        }
                    }

                    //powiadomienie o przyznaniu kuponu rabatowego
                    if (_XmlNode.Name == "NewVoucherNotification")
                    {
                        foreach (XmlNode _XmlChildNode in _XmlNode.ChildNodes)
                        {
                            if (_XmlChildNode.Name == "Header")
                            {
                                Session[NotificationVoucherHeaderPrefix + li.Value] = _XmlChildNode.ChildNodes.Count > 0 ? _XmlChildNode.ChildNodes[0].Value : string.Empty;
                            }

                            if (_XmlChildNode.Name == "Body")
                            {
                                Session[NotificationVoucherBodyPrefix + li.Value] = _XmlChildNode.ChildNodes.Count > 0 ? _XmlChildNode.ChildNodes[0].Value : string.Empty;
                            }
                        }
                    }

                    //mail z resetem hasła
                    if (_XmlNode.Name == "ResetNotification")
                    {
                        foreach (XmlNode _XmlChildNode in _XmlNode.ChildNodes)
                        {
                            if (_XmlChildNode.Name == "Header")
                            {
                                Session[NotificationResetHeaderPrefix + li.Value] = _XmlChildNode.ChildNodes.Count > 0 ? _XmlChildNode.ChildNodes[0].Value : string.Empty;
                            }

                            if (_XmlChildNode.Name == "Body")
                            {
                                Session[NotificationResetBodyPrefix + li.Value] = _XmlChildNode.ChildNodes.Count > 0 ? _XmlChildNode.ChildNodes[0].Value : string.Empty;
                            }
                        }
                    }
                }
            }

            Session["NotificationTemplatesLoaded"] = true;
        }
        catch
        { }
    }

    protected void SaveAresseeList()
    {
        int emailCol = 1;

        XmlTextWriter xmlTextWriter;

        string XmlDirectory = Settings.TemplatesDirectory + "/";

        xmlTextWriter = new XmlTextWriter(Server.MapPath(XmlDirectory + "ReportAdressees.xml"), Encoding.UTF8);
        xmlTextWriter.WriteStartDocument();

        xmlTextWriter.WriteStartElement("ReportAdresseeList");
        foreach (GridViewRow ri in gvAdressesList.Rows)
        {
            if (ri.RowType == DataControlRowType.DataRow && (((CheckBox)ri.FindControl("cbDailyReport")).Checked || ((CheckBox)ri.FindControl("cbMonthlyReport")).Checked))
            {
                xmlTextWriter.WriteStartElement("ReportAdressee");
                xmlTextWriter.WriteElementString("Email", ri.Cells[emailCol].Text);
                xmlTextWriter.WriteElementString("DailyReport", (ri.FindControl("cbDailyReport") as CheckBox).Checked.ToString());
                xmlTextWriter.WriteElementString("MonthlyReport", (ri.FindControl("cbMonthlyReport") as CheckBox).Checked.ToString());
                xmlTextWriter.WriteElementString("UserGuid", (gvAdressesList.DataKeys[ri.DataItemIndex].Value).ToString());
                xmlTextWriter.WriteEndElement();
            }
        }
        xmlTextWriter.WriteEndElement();
        xmlTextWriter.WriteEndDocument();
        xmlTextWriter.Close();
    }

    protected void SaveNotifications()
    {
        SaveNotificationsInSession();
        XmlTextWriter xmlTextWriter;

        foreach (ListItem li in ddlNotifLanguage.Items)
        {
            string XmlDirectory = Settings.TemplatesDirectory + "/";

            if (li.Value == (string)Session[DEFAULT_LANGUAGE_CULTURE])
                xmlTextWriter = new XmlTextWriter(Server.MapPath(XmlDirectory + "NotificationTemplates.xml"), Encoding.UTF8);
            else
                xmlTextWriter = new XmlTextWriter(Server.MapPath(XmlDirectory + "NotificationTemplates." + li.Value + ".xml"), Encoding.UTF8);

            xmlTextWriter.WriteStartDocument();

            xmlTextWriter.WriteStartElement("NotificationTemplates");

            xmlTextWriter.WriteStartElement("OrderConfirmation");
            xmlTextWriter.WriteElementString("Header", (String)Session[NotificationOrderHeaderPrefix + li.Value]);
            xmlTextWriter.WriteElementString("Body", (String)Session[NotificationOrderBodyPrefix + li.Value]);
            xmlTextWriter.WriteEndElement();

            xmlTextWriter.WriteStartElement("SendConfirmation");
            xmlTextWriter.WriteElementString("Header", (String)Session[NotificationSendHeaderPrefix + li.Value]);
            xmlTextWriter.WriteElementString("Body", (String)Session[NotificationSendBodyPrefix + li.Value]);
            xmlTextWriter.WriteEndElement();

            xmlTextWriter.WriteStartElement("PaymentNotification");
            xmlTextWriter.WriteElementString("Header", (String)Session[NotificationPaymentHeaderPrefix + li.Value]);
            xmlTextWriter.WriteElementString("Body", (String)Session[NotificationPaymentBodyPrefix + li.Value]);
            xmlTextWriter.WriteEndElement();
            //--------------------
            xmlTextWriter.WriteStartElement("DelayedPaymentNotification");
            xmlTextWriter.WriteElementString("Header", (String)Session[NotificationDelayedHeaderPrefix + li.Value]);
            xmlTextWriter.WriteElementString("Body", (String)Session[NotificationDelayedBodyPrefix + li.Value]);
            xmlTextWriter.WriteEndElement();
            //--------------------
            xmlTextWriter.WriteStartElement("OnlinePaymentNotification");
            xmlTextWriter.WriteElementString("Header", (String)Session[NotificationOnlineHeaderPrefix + li.Value]);
            xmlTextWriter.WriteElementString("Body", (String)Session[NotificationOnlineBodyPrefix + li.Value]);
            xmlTextWriter.WriteEndElement();

            //powiadomienie o rejestracji
            xmlTextWriter.WriteStartElement("AccountCreatedNotification");
            xmlTextWriter.WriteElementString("Header", (String)Session[NotificationConfirmHeaderPrefix + li.Value]);
            xmlTextWriter.WriteElementString("Body", (String)Session[NotificationConfirmBodyPrefix + li.Value]);
            xmlTextWriter.WriteEndElement();

            //powiadomienie o aktywacji konta
            xmlTextWriter.WriteStartElement("AccountActivatedNotification");
            xmlTextWriter.WriteElementString("Header", (String)Session[NotificationActivateHeaderPrefix + li.Value]);
            xmlTextWriter.WriteElementString("Body", (String)Session[NotificationActivateBodyPrefix + li.Value]);
            xmlTextWriter.WriteEndElement();

            //powiadomienie o przyznaniu nowego kuponu rabatowego
            xmlTextWriter.WriteStartElement("NewVoucherNotification");
            xmlTextWriter.WriteElementString("Header", (String)Session[NotificationVoucherHeaderPrefix + li.Value]);
            xmlTextWriter.WriteElementString("Body", (String)Session[NotificationVoucherBodyPrefix + li.Value]);
            xmlTextWriter.WriteEndElement();

            //mail z resetem hasła
            xmlTextWriter.WriteStartElement("ResetNotification");
            xmlTextWriter.WriteElementString("Header", (String)Session[NotificationResetHeaderPrefix + li.Value]);
            xmlTextWriter.WriteElementString("Body", (String)Session[NotificationResetBodyPrefix + li.Value]);
            xmlTextWriter.WriteEndElement();

            xmlTextWriter.WriteEndElement();
            xmlTextWriter.WriteEndDocument();
            xmlTextWriter.Close();

        }
    }

    protected void NotificationBind()
    {

        if (Session["NotificationTemplatesLoaded"] == null)
            LoadNotificationTemplates();

        string tempCulture = (string)Session["NotifLanguage"];

        switch (ddlNotificationType.SelectedValue)
        {
            case "Order":
                {
                    tbNotificationHeader.Text = Session[NotificationOrderHeaderPrefix + tempCulture] != null ? (String)Session[NotificationOrderHeaderPrefix + tempCulture] : string.Empty;
                    tbNotificationContent.Text = Session[NotificationOrderBodyPrefix + tempCulture] != null ? (String)Session[NotificationOrderBodyPrefix + tempCulture] : string.Empty;
                    Session["NotificationPrevious"] = "Order";
                    break;
                }
            case "Send":
                {
                    tbNotificationHeader.Text = Session[NotificationSendHeaderPrefix + tempCulture] != null ? (String)Session[NotificationSendHeaderPrefix + tempCulture] : string.Empty;
                    tbNotificationContent.Text = Session[NotificationSendBodyPrefix + tempCulture] != null ? (String)Session[NotificationSendBodyPrefix + tempCulture] : string.Empty;
                    Session["NotificationPrevious"] = "Send";
                    break;
                }
            case "Payment":
                {
                    tbNotificationHeader.Text = Session[NotificationPaymentHeaderPrefix + tempCulture] != null ? (String)Session[NotificationPaymentHeaderPrefix + tempCulture] : string.Empty;
                    tbNotificationContent.Text = Session[NotificationPaymentBodyPrefix + tempCulture] != null ? (String)Session[NotificationPaymentBodyPrefix + tempCulture] : string.Empty;
                    Session["NotificationPrevious"] = "Payment";
                    break;
                }
            case "Online":
                {
                    tbNotificationHeader.Text = Session[NotificationOnlineHeaderPrefix + tempCulture] != null ? (String)Session[NotificationOnlineHeaderPrefix + tempCulture] : string.Empty;
                    tbNotificationContent.Text = Session[NotificationOnlineBodyPrefix + tempCulture] != null ? (String)Session[NotificationOnlineBodyPrefix + tempCulture] : string.Empty ;
                    Session["NotificationPrevious"] = "Online";
                    break;
                }
            case "Delayed":
                {
                    tbNotificationHeader.Text = Session[NotificationDelayedHeaderPrefix + tempCulture] != null ? (String)Session[NotificationDelayedHeaderPrefix + tempCulture] : string.Empty;
                    tbNotificationContent.Text = Session[NotificationDelayedBodyPrefix + tempCulture] != null ? (String)Session[NotificationDelayedBodyPrefix + tempCulture] : string.Empty;
                    Session["NotificationPrevious"] = "Delayed";
                    break;
                }
            case "Created"://powiadomienie o rejestracji
                {
                    tbNotificationHeader.Text = Session[NotificationConfirmHeaderPrefix + tempCulture] != null ? (String)Session[NotificationConfirmHeaderPrefix + tempCulture] : string.Empty;
                    tbNotificationContent.Text = Session[NotificationConfirmBodyPrefix + tempCulture] != null ? (String)Session[NotificationConfirmBodyPrefix + tempCulture] : string.Empty;
                    Session["NotificationPrevious"] = "Created";
                    break;
                }
            case "Activated"://powiadomienie o aktywacji konta
                {
                    tbNotificationHeader.Text = Session[NotificationActivateHeaderPrefix + tempCulture] != null ? (String)Session[NotificationActivateHeaderPrefix + tempCulture] : string.Empty;
                    tbNotificationContent.Text = Session[NotificationActivateBodyPrefix + tempCulture] != null ? (String)Session[NotificationActivateBodyPrefix + tempCulture] : string.Empty;
                    Session["NotificationPrevious"] = "Activated";
                    break;
                }
            case "Voucher"://powiadomienie o przyznaniu kuponu rabatowego
                {
                    tbNotificationHeader.Text = Session[NotificationVoucherHeaderPrefix + tempCulture] != null ? (String)Session[NotificationVoucherHeaderPrefix + tempCulture] : string.Empty;
                    tbNotificationContent.Text = Session[NotificationVoucherBodyPrefix + tempCulture] != null ? (String)Session[NotificationVoucherBodyPrefix + tempCulture] : string.Empty;
                    Session["NotificationPrevious"] = "Voucher";
                    break;
                }
            case "Reset"://reset hasła
                {
                    tbNotificationHeader.Text = Session[NotificationResetHeaderPrefix + tempCulture] != null ? (String)Session[NotificationResetHeaderPrefix + tempCulture] : string.Empty;
                    tbNotificationContent.Text = Session[NotificationResetBodyPrefix + tempCulture] != null ? (String)Session[NotificationResetBodyPrefix + tempCulture] : string.Empty;
                    Session["NotificationPrevious"] = "Reset";
                    break;
                }
        }
    }

    protected void ShopContentBind()
    {
        string tmpCulture = (string)Session["ShopContentLanguage"];
        switch (ddlShopContetnType.SelectedValue)
        {
            case "Shop":
                {
                    dExternalUrl.Style.Add("display", "none");
                    heContent.Text = (string)Session[ShopContentShopPrefix + tmpCulture];
                    Session["ShopContentTypePrevios"] = "Shop";
                    break;
                }
            case "About":
                {
                    dExternalUrl.Style.Add("display", "block");
                    tbExternalUrl.Text = (string)Session[ShopContentAboutUrlPrefix + tmpCulture];
                    heContent.Text = (string)Session[ShopContentAboutPrefix + tmpCulture];
                    Session["ShopContentTypePrevios"] = "About";
                    break;
                }
            case "Contact":
                {
                    dExternalUrl.Style.Add("display", "block");
                    tbExternalUrl.Text = (string)Session[ShopContentContactUrlPrefix + tmpCulture];
                    heContent.Text = (string)Session[ShopContentContactPrefix + tmpCulture];
                    Session["ShopContentTypePrevios"] = "Contact";
                    break;
                }
            case "Delivery":
                {
                    dExternalUrl.Style.Add("display", "block");
                    tbExternalUrl.Text = (string)Session[ShopContentDeliveryUrlPrefix + tmpCulture];
                    heContent.Text = (string)Session[ShopContentDeliveryPrefix + tmpCulture];
                    Session["ShopContentTypePrevios"] = "Delivery";
                    break;
                }
            case "Shipment":
                {
                    dExternalUrl.Style.Add("display", "block");
                    tbExternalUrl.Text = (string)Session[ShopContentShipmentUrlPrefix + tmpCulture];
                    heContent.Text = (string)Session[ShopContentShipmentPrefix + tmpCulture];
                    Session["ShopContentTypePrevios"] = "Shipment";
                    break;
                }
            case "Instalments":
                {
                    dExternalUrl.Style.Add("display", "block");
                    tbExternalUrl.Text = (string)Session[ShopContentInstalmentsUrlPrefix + tmpCulture];
                    heContent.Text = (string)Session[ShopContentInstalmentsPrefix + tmpCulture];
                    Session["ShopContentTypePrevios"] = "Instalments";
                    break;
                }
            case "Regulation":
                {
                    dExternalUrl.Style.Add("display", "block");
                    tbExternalUrl.Text = (string)Session[ShopContentRegulationUrlPrefix + tmpCulture];
                    heContent.Text = (string)Session[ShopContentRegulationPrefix + tmpCulture];
                    Session["ShopContentTypePrevios"] = "Regulation";
                    break;
                }
            case "Offer":
                {
                    dExternalUrl.Style.Add("display", "block");
                    tbExternalUrl.Text = (string)Session[ShopContentOfferUrlPrefix + tmpCulture];
                    heContent.Text = (string)Session[ShopContentOfferPrefix + tmpCulture];
                    Session["ShopContentTypePrevios"] = "Offer";
                    break;
                }

        }
    }

    protected void SaveNotificationsInSession()
    {
        string tempCulture = (string)Session["NotifLanguage"];

        switch ((string)Session["NotificationPrevious"])
        {
            case "Order":
                {
                    Session[NotificationOrderBodyPrefix + tempCulture] = tbNotificationContent.Text;
                    Session[NotificationOrderHeaderPrefix + tempCulture] = tbNotificationHeader.Text;
                    break;
                }
            case "Send":
                {
                    Session[NotificationSendBodyPrefix + tempCulture] = tbNotificationContent.Text;
                    Session[NotificationSendHeaderPrefix + tempCulture] = tbNotificationHeader.Text;
                    break;
                }
            case "Payment":
                {
                    Session[NotificationPaymentBodyPrefix + tempCulture] = tbNotificationContent.Text;
                    Session[NotificationPaymentHeaderPrefix + tempCulture] = tbNotificationHeader.Text;
                    break;
                }
            case "Delayed":
                {
                    Session[NotificationDelayedBodyPrefix + tempCulture] = tbNotificationContent.Text;
                    Session[NotificationDelayedHeaderPrefix + tempCulture] = tbNotificationHeader.Text;
                    break;
                }
            case "Online":
                {
                    Session[NotificationOnlineBodyPrefix + tempCulture] = tbNotificationContent.Text;
                    Session[NotificationOnlineHeaderPrefix + tempCulture] = tbNotificationHeader.Text;
                    break;
                }
            case "Created"://rejestracja
                {
                    Session[NotificationConfirmBodyPrefix + tempCulture] = tbNotificationContent.Text;
                    Session[NotificationConfirmHeaderPrefix + tempCulture] = tbNotificationHeader.Text;
                    break;
                }
            case "Activated"://aktywacjia
                {
                    Session[NotificationActivateBodyPrefix + tempCulture] = tbNotificationContent.Text;
                    Session[NotificationActivateHeaderPrefix + tempCulture] = tbNotificationHeader.Text;
                    break;
                }
            case "Voucher"://kupon
                {
                    Session[NotificationVoucherBodyPrefix + tempCulture] = tbNotificationContent.Text;
                    Session[NotificationVoucherHeaderPrefix + tempCulture] = tbNotificationHeader.Text;
                    break;
                }
            case "Reset"://kupon
                {
                    Session[NotificationResetBodyPrefix + tempCulture] = tbNotificationContent.Text;
                    Session[NotificationResetHeaderPrefix + tempCulture] = tbNotificationHeader.Text;
                    break;
                }
        }
    }

    protected void SaveShopContentInSession()
    {
        string tempCulture = (string)Session["ShopContentLanguage"];
        switch ((string)Session["ShopContentTypePrevios"])
        {
            case "Shop":
                {
                    Session[ShopContentShopPrefix + tempCulture] = heContent.Text;
                    break;
                }
            case "About":
                {
                    Session[ShopContentAboutUrlPrefix + tempCulture] = tbExternalUrl.Text;
                    Session[ShopContentAboutPrefix + tempCulture] = heContent.Text;
                    break;
                }
            case "Contact":
                {
                    Session[ShopContentContactUrlPrefix + tempCulture] = tbExternalUrl.Text;
                    Session[ShopContentContactPrefix + tempCulture] = heContent.Text;
                    break;
                }
            case "Delivery":
                {
                    Session[ShopContentDeliveryUrlPrefix + tempCulture] = tbExternalUrl.Text;
                    Session[ShopContentDeliveryPrefix + tempCulture] = heContent.Text;
                    break;
                }
            case "Shipment":
                {
                    Session[ShopContentShipmentUrlPrefix + tempCulture] = tbExternalUrl.Text;
                    Session[ShopContentShipmentPrefix + tempCulture] = heContent.Text;
                    break;
                }
            case "Instalments":
                {
                    Session[ShopContentInstalmentsUrlPrefix + tempCulture] = tbExternalUrl.Text;
                    Session[ShopContentInstalmentsPrefix + tempCulture] = heContent.Text;
                    break;
                }
            case "Regulation":
                {
                    Session[ShopContentRegulationUrlPrefix + tempCulture] = tbExternalUrl.Text;
                    Session[ShopContentRegulationPrefix + tempCulture] = heContent.Text;
                    break;
                }
            case "Offer":
                {
                    Session[ShopContentOfferUrlPrefix + tempCulture] = tbExternalUrl.Text;
                    Session[ShopContentOfferPrefix + tempCulture] = heContent.Text;
                    break;
                }
        }
    }

    private void gvAdressesListDataBind()
    {
        ReportMailingList = ReportsManager.Instance.GetReportAdresseeList();
        gvAdressesList.DataSource = OrderManager.Instance.GetUsers(true, "", "", "", true, false, CommonTools.GetCurrentLanguageID());
        gvAdressesList.DataBind();
    }

    private void BindLanguagesLists()
    {
        List<Language> languages = ReportsManager.Instance.GetAvailableLanguages(true);
        ddlNotifLanguage.DataSource = languages;
        ddlShopContentLanguage.DataSource = languages;
        ddlPageSettingsLanguage.DataSource = languages;

        ddlNotifLanguage.DataBind();
        ddlShopContentLanguage.DataBind();
        ddlPageSettingsLanguage.DataBind();

        for (int i = 0; i < languages.Count; i++)
        {
            if (languages[i].IsDefault) { Session[DEFAULT_LANGUAGE_CULTURE] = languages[i].Culture; break; }
        }

    }

    private void PageSettingsBind()
    {
        string tmpCulture = (string)Session["PageSettingsLanguage"];
        tbTitleTag.Text = (string)Session[TitleTagPrefix + tmpCulture];
        tbMetaTagDescription.Text = (string)Session[MetaTagDescriptionPrefix + tmpCulture];
        tbMetaTagKeywords.Text = (string)Session[MetaTagKeywordsPrefix + tmpCulture];
        hePageFooter.Text = (string)Session[PageFooterPrefix + tmpCulture];
    }

    private void SavePageSettingsInSession()
    {
        string tmpCulture = (string)Session["PageSettingsLanguage"];
        Session[TitleTagPrefix + tmpCulture] = tbTitleTag.Text;
        Session[MetaTagDescriptionPrefix + tmpCulture] = tbMetaTagDescription.Text;
        Session[MetaTagKeywordsPrefix + tmpCulture] = tbMetaTagKeywords.Text;
        Session[PageFooterPrefix + tmpCulture] = hePageFooter.Text;
    }

    private void PartnersDataBind()
    {
        rptPartners.DataSource = ReportsManager.Instance.GetPartners(true);
        rptPartners.DataBind();
    }

    private void ClearNewPartnerRow()
    {
        hfPartnerName.Value = string.Empty;
        tbPartnersWeight.Text = string.Empty;
        cbPartnerIsActive.Checked = true;
        tbPartnersUrl.Text = string.Empty;
        btnPartnersAdd.Text = Resources.Language.Add;
        Session[EDITED_PARTNER] = null;
    }

    private void AddScriptsAndAttributes()
    {
        ClientScript.RegisterStartupScript(this.GetType(), "SetTextValue", string.Format("<script type=\"text/javascript\"> hideMarkersText='{0}'; showMarkersText='{1}'; </script>", Resources.Language.TagListHide, Resources.Language.TagList));
    }

    private void BindThemes()
    {
        DirectoryInfo dInfo = new DirectoryInfo(System.Web.HttpContext.Current.Server.MapPath("../App_Themes"));
        DirectoryInfo[] dArrInfo = dInfo.GetDirectories();
        List<Theme> list = new List<Theme>();
        foreach (DirectoryInfo sDirectory in dArrInfo)
        {
            Theme temp = new Theme(sDirectory.Name);
            list.Add(temp);
        }

        ddlThemes.DataSource = list;
        ddlThemes.DataBind();
        ddlThemes.SelectedValue = Settings.Theme;
    }

    protected void VatRatesDataBind()
    {
        rptVatRates.DataSource = ProductsManager.Instance.GetVatRates();
        rptVatRates.DataBind();
    }

    #endregion METHODS

    #region EVENTS

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        CtrlShowInfo.ShowInfo(false, Resources.Language.CmsConfigMessage1);
        _DataBind();
        NotificationBind();
        ShopContentBind();
        PageSettingsBind();
    }

    protected void btnSaveConfig_Click(object sender, EventArgs e)
    {
        SaveShopContentInSession();
        SavePageSettingsInSession();

        ShopModel.Settings.CompanyName = tbCompanyName.Text;
        ShopModel.Settings.CompanyOwner = tbCompanyOwner.Text;
        ShopModel.Settings.CompanyAccount = tbCompanyAccount.Text;
        ShopModel.Settings.CompanyNIP = tbCompanyNIP.Text;
        ShopModel.Settings.CompanyStreet = tbCompanyStreet.Text;
        ShopModel.Settings.CompanyTown = tbCompanyTown.Text;
        ShopModel.Settings.CompanyZipCode = tbCompanyZipCode.Text;
        ShopModel.Settings.CompanyEmail = tbCompanyEmail.Text;
        ShopModel.Settings.CompanyPhoneNumber = tbCompanyPhoneNumber.Text;
        ShopModel.Settings.CompanyOnlineContact = tbCompanyOnlineContact.Text;

        ShopModel.Settings.CompanyMailHost = tbCompanyMailHost.Text;
        ShopModel.Settings.CompanyMailLogin = tbCompanyMailLogin.Text;
        ShopModel.Settings.CompanyMailPassword = tbCompanyMailPassword.Text;
        ShopModel.Settings.CompanyEmailDisplayName = tbCompanyMailDisplayedName.Text;

        ShopModel.Settings.ShipmentAgreement = tbShipmentAgreement.Text;

        ShopModel.Settings.InvoiceFooter = tbInvoiceFooter.Text;
        ShopModel.Settings.InvoiceNumberMask = tbInvoiceNumberMask.Text;

        ShopModel.Settings.AvailableCountRange = Convert.ToInt32(tbAvailableCountRange.Text);
        ShopModel.Settings.RegistrationConfirmationMail = cbMailConfirmation.Checked;

        ShopModel.Settings.ShowProductsToCompare = cbDisplayingProductscomparison.Checked;
        ShopModel.Settings.ShowBestsellers = cbDisplayingBestsellers.Checked;
        ShopModel.Settings.ShowNews = cbDisplayingNews.Checked;
        ShopModel.Settings.ShowPartners = cbDisplayingPartners.Checked;
        ShopModel.Settings.ShowAvailableCount = cbShowAvailableCount.Checked;
        ShopModel.Settings.ShowCrossSell = cbShowCrossSell.Checked;
        ShopModel.Settings.Theme = ddlThemes.SelectedValue;

        decimal tmpVatM = 0;
        if (decimal.TryParse(tbVatAmount.Text, out tmpVatM))
        {
            ShopModel.Settings.ShipmentVatRateValue = (tmpVatM / (decimal)100);
        }

        if (!ReportsManager.Instance.SaveSettings())
        {
            CtrlShowInfo.ShowInfo(false, Resources.Language.Cms_ErrorWhileSavingInDataBase);
            return;
        }
        else
            ShopBLL.ReportsManager.Instance.GetSettings();

        XmlTextWriter xmlTextWriter;

        string XmlDirectory = Settings.TemplatesDirectory + "/";

        foreach (ListItem li in ddlShopContentLanguage.Items)
        {
            if (li.Value == (string)Session[DEFAULT_LANGUAGE_CULTURE])
                xmlTextWriter = new XmlTextWriter(Server.MapPath(XmlDirectory + "ContentTemplates.xml"), Encoding.UTF8);
            else
                xmlTextWriter = new XmlTextWriter(Server.MapPath(XmlDirectory + "ContentTemplates." + li.Value + ".xml"), Encoding.UTF8);

            xmlTextWriter.WriteStartDocument();

            xmlTextWriter.WriteStartElement("ContentTemplates");

            xmlTextWriter.WriteStartElement("Shop");
            xmlTextWriter.WriteElementString("Content", (String)Session[ShopContentShopPrefix + li.Value]);
            xmlTextWriter.WriteEndElement();

            xmlTextWriter.WriteStartElement("About");
            xmlTextWriter.WriteElementString("Content", (String)Session[ShopContentAboutPrefix + li.Value]);
            xmlTextWriter.WriteElementString("Url", (String)Session[ShopContentAboutUrlPrefix + li.Value]);
            xmlTextWriter.WriteEndElement();

            xmlTextWriter.WriteStartElement("Contact");
            xmlTextWriter.WriteElementString("Content", (String)Session[ShopContentContactPrefix + li.Value]);
            xmlTextWriter.WriteElementString("Url", (String)Session[ShopContentContactUrlPrefix + li.Value]);
            xmlTextWriter.WriteEndElement();

            xmlTextWriter.WriteStartElement("Delivery");
            xmlTextWriter.WriteElementString("Content", (String)Session[ShopContentDeliveryPrefix + li.Value]);
            xmlTextWriter.WriteElementString("Url", (String)Session[ShopContentDeliveryUrlPrefix + li.Value]);
            xmlTextWriter.WriteEndElement();

            xmlTextWriter.WriteStartElement("Shipment");
            xmlTextWriter.WriteElementString("Content", (String)Session[ShopContentShipmentPrefix + li.Value]);
            xmlTextWriter.WriteElementString("Url", (String)Session[ShopContentShipmentUrlPrefix + li.Value]);
            xmlTextWriter.WriteEndElement();

            xmlTextWriter.WriteStartElement("Instalments");
            xmlTextWriter.WriteElementString("Content", (String)Session[ShopContentInstalmentsPrefix + li.Value]);
            xmlTextWriter.WriteElementString("Url", (String)Session[ShopContentInstalmentsUrlPrefix + li.Value]);
            xmlTextWriter.WriteEndElement();

            xmlTextWriter.WriteStartElement("Regulation");
            xmlTextWriter.WriteElementString("Content", (String)Session[ShopContentRegulationPrefix + li.Value]);
            xmlTextWriter.WriteElementString("Url", (String)Session[ShopContentRegulationUrlPrefix + li.Value]);
            xmlTextWriter.WriteEndElement();

            xmlTextWriter.WriteStartElement("Offer");
            xmlTextWriter.WriteElementString("Content", (String)Session[ShopContentOfferPrefix + li.Value]);
            xmlTextWriter.WriteElementString("Url", (String)Session[ShopContentOfferUrlPrefix + li.Value]);
            xmlTextWriter.WriteEndElement();

            xmlTextWriter.WriteStartElement("TitleTag");
            xmlTextWriter.WriteElementString("Content", (String)Session[TitleTagPrefix + li.Value]);
            xmlTextWriter.WriteEndElement();

            xmlTextWriter.WriteStartElement("MetaTagDescription");
            xmlTextWriter.WriteElementString("Content", (String)Session[MetaTagDescriptionPrefix + li.Value]);
            xmlTextWriter.WriteEndElement();

            xmlTextWriter.WriteStartElement("MetaTagKeywords");
            xmlTextWriter.WriteElementString("Content", (String)Session[MetaTagKeywordsPrefix + li.Value]);
            xmlTextWriter.WriteEndElement();

            xmlTextWriter.WriteStartElement("PageFooter");
            xmlTextWriter.WriteElementString("Content", (String)Session[PageFooterPrefix + li.Value]);
            xmlTextWriter.WriteEndElement();


            xmlTextWriter.WriteEndElement();
            xmlTextWriter.WriteEndDocument();
            xmlTextWriter.Close();

        }

        SaveAresseeList();
        SaveNotifications();

        int InvoiceNr = -1;
        if (int.TryParse(tbInvoiceLastNumber.Text, out InvoiceNr))
            OrderManager.Instance.UpdateInvoiceNumber(InvoiceNr);

        CtrlShowInfo.ShowInfo(false, Resources.Language.CmsConfigMessage2);

        _DataBind();
    }

    protected void ddlNotificationTypeSelectedIndexChanged(object sender, EventArgs e)
    {
        SaveNotificationsInSession();
        NotificationBind();
    }

    protected void ddlShopContentTypeSelectedIndexChanged(object sender, EventArgs e)
    {
        SaveShopContentInSession();
        ShopContentBind();
    }

    protected void btnTestNotification_Click(object sender, EventArgs e)
    {
        SaveNotificationsInSession();
        if (btnTestNotification.Text == Resources.Language.ShowNotification)
        {
            btnTestNotification.Text = Resources.Language.HideNotification;
            dTestNotification.Visible = true;
            dTestNotificationHeader.Visible = true;

            edit.Visible = false;

            Realization _realization = OrderManager.Instance.GetRealizationAllDetails(OrderManager.Instance.GetLastRealization().IDRealization, ShopBLL.CommonTools.CurrentLanguageID);
            NotificationTemplate notification = new NotificationTemplate();

            string tempCulture = (string)Session["NotifLanguage"];
            switch (ddlNotificationType.SelectedValue)
            {
                case "Order":
                    {
                        notification.Body = (string)Session[NotificationOrderBodyPrefix + tempCulture];
                        notification.Header = (string)Session[NotificationOrderHeaderPrefix + tempCulture];
                        break;
                    }
                case "Send":
                    {
                        notification.Body = (string)Session[NotificationSendBodyPrefix + tempCulture];
                        notification.Header = (string)Session[NotificationSendHeaderPrefix + tempCulture];
                        break;
                    }
                case "Payment":
                    {
                        notification.Body = (string)Session[NotificationPaymentBodyPrefix + tempCulture];
                        notification.Header = (string)Session[NotificationPaymentHeaderPrefix + tempCulture];
                        break;
                    }
                case "Online":
                    {
                        notification.Body = (string)Session[NotificationOnlineBodyPrefix + tempCulture];
                        notification.Header = (string)Session[NotificationOnlineHeaderPrefix + tempCulture];
                        break;
                    }
                case "Created"://rejestracja
                    {
                        notification.Body = (string)Session[NotificationConfirmBodyPrefix + tempCulture];
                        notification.Header = (string)Session[NotificationConfirmHeaderPrefix + tempCulture];
                        break;
                    }
                case "Activated"://aktywacja
                    {
                        notification.Body = (string)Session[NotificationActivateBodyPrefix + tempCulture];
                        notification.Header = (string)Session[NotificationActivateHeaderPrefix + tempCulture];
                        break;
                    }
                case "Voucher"://aktywacja
                    {
                        notification.Body = (string)Session[NotificationVoucherBodyPrefix + tempCulture];
                        notification.Header = (string)Session[NotificationVoucherHeaderPrefix + tempCulture];
                        break;
                    }
                case "Reset"://aktywacja
                    {
                        notification.Body = (string)Session[NotificationResetBodyPrefix + tempCulture];
                        notification.Header = (string)Session[NotificationResetHeaderPrefix + tempCulture];
                        break;
                    }
            }
            NotificationTemplate nt = ReportsManager.Instance.FillNotificationTemplate(_realization, notification, null);
            dTestNotification.InnerHtml =  nt.Body;
            dTestNotificationHeader.InnerHtml = "<strong>" + Resources.Language.Header + ": " + nt.Header + "</strong>";
        }
        else
        {
            btnTestNotification.Text = Resources.Language.ShowNotification;
            dTestNotification.Visible = false;
            dTestNotificationHeader.Visible = false;
            edit.Visible = true;
        }
    }

    protected void ddlNotifLanguage_IndexChanged(object sender, EventArgs e)
    {
        SaveNotificationsInSession();
        Session["NotifLanguage"] = ddlNotifLanguage.SelectedValue;
        NotificationBind();
    }

    protected void ddlShopContentLanguage_IndexChanged(object sender, EventArgs e)
    {
        SaveShopContentInSession();
        Session["ShopContentLanguage"] = ddlShopContentLanguage.SelectedValue;
        ShopContentBind();
    }

    protected void ddlPageSettingsLanguage_IndexChanged(object sender, EventArgs e)
    {
        SavePageSettingsInSession();
        Session["PageSettingsLanguage"] = ddlPageSettingsLanguage.SelectedValue;
        PageSettingsBind();
    }

    protected void gvAdressesListDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            CheckBox cbDaily = (CheckBox)e.Row.FindControl("cbDailyReport");
            CheckBox cbMonthly = (CheckBox)e.Row.FindControl("cbMonthlyReport");
            foreach (ReportAdressee ra in ReportMailingList)
            {
                if (ra.UserGuid.ToString() == ((ShopUser)(e.Row.DataItem)).ProviderUserKey.ToString())
                {
                    cbDaily.Checked = ra.DailyReport;
                    cbMonthly.Checked = ra.MonthlyReport;
                    break;
                }
            }
        }
    }

    protected void PartnersCommand(object sender, CommandEventArgs e)
    {
        switch (e.CommandName)
        {
            case "Cancel":
                {
                    ClearNewPartnerRow();
                    break;
                }
            case "Delete":
                {
                    if (!ReportsManager.Instance.DeletePartner(Convert.ToInt32(e.CommandArgument)))
                        CtrlShowInfo.ShowInfo(true, Resources.Language.Error_PleaseTryLater);
                    break;
                }
            case "Edit":
                {
                    Partner partner = ReportsManager.Instance.GetPartner(Convert.ToInt32(e.CommandArgument));
                    ViewState[EDITED_PARTNER] = partner.PartnerID;
                    tbPartnersWeight.Text = partner.Weight.ToString();
                    cbPartnerIsActive.Checked = partner.IsActive;
                    tbPartnersUrl.Text = partner.PartnersUrl;
                    hfPartnerName.Value = partner.Name;
                    btnPartnersAdd.Text = Resources.Language.Save;
                    break;
                }
            case "Save":
                {
                    int weight = 0;
                    if (int.TryParse(tbPartnersWeight.Text, out weight) && (fuPartners.HasFile || !string.IsNullOrEmpty(hfPartnerName.Value)) && !string.IsNullOrEmpty(tbPartnersUrl.Text))
                    {
                        if (!ReportsManager.Instance.SavePartner(new Partner(ViewState[EDITED_PARTNER] == null ? -1 : (int)ViewState[EDITED_PARTNER], hfPartnerName.Value, weight, cbPartnerIsActive.Checked, tbPartnersUrl.Text), fuPartners.FileBytes, fuPartners.FileName))
                            CtrlShowInfo.ShowInfo(true, Resources.Language.Error_PleaseTryLater);
                        else
                            CtrlShowInfo.ShowInfo(false, Resources.Language.cmsMessageDataSaveSuccessful);
                        ClearNewPartnerRow();
                    }
                    else
                    {
                        CtrlShowInfo.ShowInfo(true, Resources.Language.Error_NotAllFieldsFilled);
                    }
                    break;
                }
        }
        ViewState[ACTIVE_TAB] = 6;
        PartnersDataBind();
    }

    protected void VatRatesCommand(object sender, CommandEventArgs e)
    {
        VatRate tmp;

        switch (e.CommandName)
        {
            case "Edit":
                tmp = ProductsManager.Instance.GetVatRate(Convert.ToInt32(e.CommandArgument));
                hfVatRateID.Value = tmp.VatRateID.Value.ToString();
                tbVatRateValue.Text = tmp.Value.ToString();
                tbVatRateDefault.Checked = tmp.Default;
                btnVatRatesSave.Text = Resources.Language.Save;
                break;
            case "Save":
                int vatRateID = -1;

                if (int.TryParse(hfVatRateID.Value, out vatRateID))
                    tmp = new VatRate(vatRateID, decimal.Parse(tbVatRateValue.Text), tbVatRateDefault.Checked);
                else
                    tmp = new VatRate(null, decimal.Parse(tbVatRateValue.Text), tbVatRateDefault.Checked);

                if (!ProductsManager.Instance.SaveVatRate(tmp))
                    CtrlShowInfo.ShowInfo(Resources.Language.VatRateSavingError, true);
                else
                {
                    hfVatRateID.Value = "";
                    tbVatRateValue.Text = "";
                    tbVatRateDefault.Checked = false;
                    btnVatRatesSave.Text = Resources.Language.Add;
                }
                break;
            case "Cancel":
                hfVatRateID.Value = "";
                tbVatRateValue.Text = "";
                tbVatRateDefault.Checked = false;
                btnVatRatesSave.Text = Resources.Language.Add;
                break;
            case "Delete":

                if (!ProductsManager.Instance.DeleteVatRate(Convert.ToInt32(e.CommandArgument)))
                    CtrlShowInfo.ShowInfo(Resources.Language.VatRateDeletingError, true);

                break;
        }

        VatRatesDataBind();
    }

    #endregion EVENTS

    #region PAGE EVENTS

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["tab"] != null)
        {
            tcConfig.ActiveTabIndex = Convert.ToInt16(Request.QueryString["tab"]);
            CtrlShowInfo.ShowInfo(false, Resources.Language.LanguageDataChanged);
        }

        if (!IsPostBack || Session["ConfigNotSaved"] == null)
        {
            BindLanguagesLists();
            Session["ConfigNotSaved"] = true;
            Session["NotifLanguage"] = ddlNotifLanguage.Items[0].Value;
            Session["ShopContentLanguage"] = ddlShopContentLanguage.Items[0].Value;
            Session["PageSettingsLanguage"] = ddlPageSettingsLanguage.Items[0].Value;
            _DataBind();
            NotificationBind();
            ShopContentBind();
            PageSettingsBind();
            BindThemes();
			VatRatesDataBind();
        }
    }
    
    protected void Page_PreRender(object sender, EventArgs e)
    {
        AddScriptsAndAttributes();
        if (ViewState[ACTIVE_TAB] != null)
            tcConfig.ActiveTabIndex = (int)ViewState[ACTIVE_TAB];
    }

    #endregion PAGE EVENTS

}

